﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SpotItServerAPI;
using SpotItComponents;
using Vitruvian.Services;
using System.Threading;

namespace AppLayer
{
    public class Services
    {
        int maxWaitingTime = 60000;
        public SymbolManager symbolManager { get; set; }
        public AccountManager accountManager { get; set; }
        public CategoryManager categoryManager { get; set; }
        public DeckManager deckManager { get; set; }
        public GameManager gameManager { get; set; }
        //public SessionManager sessionManager { get; set; }


        public Services()
        {
        }

        public void WaitForAllDistributedServices()
        {
            symbolManager = GetDistributedService<SymbolManager>();
            accountManager = GetDistributedService<AccountManager>();
            categoryManager = GetDistributedService<CategoryManager>();
            deckManager = GetDistributedService<DeckManager>();
            gameManager = GetDistributedService<GameManager>();
            //sessionManager = GetDistributedService<SessionManager>();
        }

        public T GetDistributedService<T>() where T : IService
        {
            T service = default(T);

            int maxTime = maxWaitingTime;
            while ((service = ServiceRegistry.GetPreferredService<T>()) == null && maxTime > 0)
            {
                Thread.Sleep(100);
                maxTime -= 100;
            }
            return service;
        }

    }
}
